import os
import re
from typing import List, Tuple

from python_part.PolicyStoreModule import PolicyStoreModule
from python_part.policy_receive.line_solver.comment_solver import CommentSolver
from python_part.policy_receive.line_solver.line_solver_base import LineSolverBase
from python_part.policy_receive.line_solver.link_solver import LinkSolver
from python_part.policy_receive.line_solver.set_solver import SetSolver

from GlobaLogger import Log
log = Log(__name__).getlog()

class PolicyReceiveModule:
    def __init__(self, line_solver_list: Tuple[LineSolverBase] = ()):
        self.line_solver_tuple: Tuple[LineSolverBase] = line_solver_list
        pass

    def accept_policy(self, policy_file:str="policy.txt"):
        with open(policy_file, 'r') as f:
            while True:
                # Get next line from file
                line = f.readline()
                # If line is empty then end of file reached
                if not line:
                    break
                if len(line) < 3:
                    continue
                log.info(f"开始处理:{line.strip()}")
                self.analyze_one_line(line)

    def analyze_one_line(self, line_content):
        """
        1. 判断改行属于哪一种
        2. 进行解析
        :param line_content:
        :return:
        """
        line_type = -1
        for i, i_solver in enumerate(self.line_solver_tuple):
            if i_solver.check_is_this(line_content):
                if line_type == -1:
                    line_type = i
                else:
                    line_type = -1
                    break
        if line_type == -1:
            log.error(f"遇到未知语句: {line_content}")

        is_ok = self.line_solver_tuple[line_type].solve_one(line_content)
        if not is_ok:
            log.error(f"语句解析错误: {line_content}")


if __name__ == "__main__":
    policy_save_path = os.path.join('.', 'for_code_running', 'policy_store')
    tmp_path = os.path.join('.', 'for_code_running', 'tmp')
    policy_store_module = PolicyStoreModule(policy_save_path, tmp_path)

    line_solver_list = [CommentSolver, LinkSolver, SetSolver]
    line_solver_list = [i(policy_store_module.get_all_policy,
        policy_store_module.update_entity_path_to_rwclass_id,
        policy_store_module.update_rwclass_id_to_rwclass,
        policy_store_module.delete_entity_path_to_rwclass_id,
        policy_store_module.delete_rwclass_id_to_rwclass)for i in line_solver_list]
    PolicyReceiveModule(tuple(line_solver_list)).accept_policy()

    policy_store_module.show_all()



# strs = """
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/test_read_file", 11
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/test_read_file2", 12
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/object_change_test", 13
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/test_file1.txt", 1
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/test_file2.txt", 2
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/test_file3.txt", 3
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing/test_file4.txt", 4
# link "/home/ubuntu/python_serves/eBPF_Access_Control/Black_box_Testing", 5
#
# set owner_class_id of 1 by 11
# set who_can_read_this of 1 by 15, 12
# set who_can_write_this of 1 by 11, 12, 13, 14, 15
# set who_can_read_this of 1 by 15, 12a
# set who_can_read_this of 1 by 15, 1a2
# set who_can_read_this of 1 by 1a5, 12
# set who_can_read_this of 1 by a15, 12
# """
# for line in strs.splitlines():
#     result = pattern.match(line)
#     print(result, end=" ")
#     if result:
#         ans_list = result.groups()
#         print(ans_list)




